using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net.Mail;
using System.Collections.Generic;
using System.Text;

namespace Imgalib.Utility
{
    /// <summary>
    /// Utility class for sending email
    /// </summary>
    public class Emailer
    {
        /// <summary>
        /// SMTP Host URL/IP
        /// </summary>
        public String SMTP_HOST
        {
            get;
            set;
        }

        /// <summary>
        /// SMTP Host username to login
        /// </summary>
        public String SMTP_USER_NAME
        {
            get;
            set;
        }

        /// <summary>
        /// SMTP Host password to login
        /// </summary>
        public String SMTP_PASSWORD
        {
            get;
            set;
        }

        /// <summary>
        /// Create an instance with host, username and password
        /// </summary>
        /// <param name="hostName">SMTP Host URL/IP</param>
        /// <param name="username">SMTP Host username to login</param>
        /// <param name="password">SMTP Host password to login</param>
        public Emailer(String hostName, String username, String password)
        {
            this.SMTP_HOST = hostName;
            this.SMTP_USER_NAME = username;
            this.SMTP_PASSWORD = password;
        }

        /// <summary>
        /// Create an instance
        /// </summary>
        public Emailer() { }

        /// <summary>
        /// Send Email
        /// </summary>
        /// <param name="to">Receiver email id</param>
        /// <param name="from">Sender email id</param>
        /// <param name="fromName">Sender name</param>
        /// <param name="subject">Smail subject</param>
        /// <param name="body">Email content</param>
        /// <param name="message">Error message in case of exception while sending</param>
        /// <returns>send success/fail status</returns>
        public bool SendMail(String to, string from, String fromName, String subject, String body, ref String message)
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage emailMessage = new MailMessage();

            try
            {
                MailAddress fromAddress = new MailAddress(from, fromName);
                //mail server
                smtpClient.Host = SMTP_HOST;
                smtpClient.Credentials = new System.Net.NetworkCredential(SMTP_USER_NAME, SMTP_PASSWORD);

                //From address will be given as a MailAddress Object
                emailMessage.From = fromAddress;

                // To address collection of MailAddress
                emailMessage.To.Add(to);
                emailMessage.Subject = subject;

                // CC and BCC optional

                //Body can be Html or text format
                //Specify true if it  is html message
                emailMessage.IsBodyHtml = true;

                // Message body content
                emailMessage.Body = body;

                //Send SMTP mail
                smtpClient.Send(emailMessage);

                message = @"Email successfully sent.";
            }
            catch (SmtpException smtpEx)
            {
                message = smtpEx.Message;
                return false;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return false;
            }
            return true;
        }
    }
}